package com.luo.backend.dto;

import com.luo.backend.entity.Food;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品响应DTO
 */
public class FoodResponse {
    
    private Long id;
    private Long merchantId;
    private Long categoryId;
    private String name;
    private String description;
    private String image;
    private BigDecimal price;
    private BigDecimal originalPrice;
    private Integer stock;
    private Integer soldCount;
    private BigDecimal rating;
    private Integer ratingCount;
    private Integer isHot;
    private Integer isRecommended;
    private List<String> tags;
    private List<String> spiceLevels;
    private Integer status;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime createdAt;
    
    // 关联信息
    private CategoryInfo category;
    private MerchantInfo merchant;
    
    // 构造函数
    public FoodResponse() {}
    
    public FoodResponse(Food food) {
        this.id = food.getId();
        this.merchantId = food.getMerchantId();
        this.categoryId = food.getCategoryId();
        this.name = food.getName();
        this.description = food.getDescription();
        this.image = food.getImage();
        this.price = food.getPrice();
        this.originalPrice = food.getOriginalPrice();
        this.stock = food.getStock();
        this.soldCount = food.getSoldCount();
        this.rating = food.getRating();
        this.ratingCount = food.getRatingCount();
        this.isHot = food.getIsHot();
        this.isRecommended = food.getIsRecommended();
        this.status = food.getStatus();
        this.createdAt = food.getCreatedAt();
        
        // 解析JSON字段
        this.tags = parseJsonToStringList(food.getTags());
        this.spiceLevels = parseJsonToStringList(food.getSpiceLevels());
    }
    
    private List<String> parseJsonToStringList(String json) {
        if (json == null || json.trim().isEmpty()) {
            return new ArrayList<>();
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(json, new TypeReference<List<String>>() {});
        } catch (JsonProcessingException e) {
            return new ArrayList<>();
        }
    }
    
    // 内部类 - 分类信息
    public static class CategoryInfo {
        private Long id;
        private String name;
        
        public CategoryInfo(Long id, String name) {
            this.id = id;
            this.name = name;
        }
        
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
    }
    
    // 内部类 - 商家信息
    public static class MerchantInfo {
        private Long id;
        private String name;
        private String logo;
        private BigDecimal minOrderAmount;
        private BigDecimal deliveryFee;
        
        public MerchantInfo(Long id, String name, BigDecimal minOrderAmount, BigDecimal deliveryFee) {
            this.id = id;
            this.name = name;
            this.minOrderAmount = minOrderAmount;
            this.deliveryFee = deliveryFee;
        }
        
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getLogo() { return logo; }
        public void setLogo(String logo) { this.logo = logo; }
        public BigDecimal getMinOrderAmount() { return minOrderAmount; }
        public void setMinOrderAmount(BigDecimal minOrderAmount) { this.minOrderAmount = minOrderAmount; }
        public BigDecimal getDeliveryFee() { return deliveryFee; }
        public void setDeliveryFee(BigDecimal deliveryFee) { this.deliveryFee = deliveryFee; }
    }
    
    // Getters and Setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    
    public Long getMerchantId() { return merchantId; }
    public void setMerchantId(Long merchantId) { this.merchantId = merchantId; }
    
    public Long getCategoryId() { return categoryId; }
    public void setCategoryId(Long categoryId) { this.categoryId = categoryId; }
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    
    public String getImage() { return image; }
    public void setImage(String image) { this.image = image; }
    
    public BigDecimal getPrice() { return price; }
    public void setPrice(BigDecimal price) { this.price = price; }
    
    public BigDecimal getOriginalPrice() { return originalPrice; }
    public void setOriginalPrice(BigDecimal originalPrice) { this.originalPrice = originalPrice; }
    
    public Integer getStock() { return stock; }
    public void setStock(Integer stock) { this.stock = stock; }
    
    public Integer getSoldCount() { return soldCount; }
    public void setSoldCount(Integer soldCount) { this.soldCount = soldCount; }
    
    public BigDecimal getRating() { return rating; }
    public void setRating(BigDecimal rating) { this.rating = rating; }
    
    public Integer getRatingCount() { return ratingCount; }
    public void setRatingCount(Integer ratingCount) { this.ratingCount = ratingCount; }
    
    public Integer getIsHot() { return isHot; }
    public void setIsHot(Integer isHot) { this.isHot = isHot; }
    
    public Integer getIsRecommended() { return isRecommended; }
    public void setIsRecommended(Integer isRecommended) { this.isRecommended = isRecommended; }
    
    public List<String> getTags() { return tags; }
    public void setTags(List<String> tags) { this.tags = tags; }
    
    public List<String> getSpiceLevels() { return spiceLevels; }
    public void setSpiceLevels(List<String> spiceLevels) { this.spiceLevels = spiceLevels; }
    
    public Integer getStatus() { return status; }
    public void setStatus(Integer status) { this.status = status; }
    
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    
    public CategoryInfo getCategory() { return category; }
    public void setCategory(CategoryInfo category) { this.category = category; }
    
    public MerchantInfo getMerchant() { return merchant; }
    public void setMerchant(MerchantInfo merchant) { this.merchant = merchant; }
}
